package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.Purchase;
import cn.gson.boot.model.pojos.PurchaseDetails;
import cn.gson.boot.model.pojos.Supplier;
import cn.gson.boot.model.pojos.SupplierContact;
import cn.gson.boot.model.service.PurchaseService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/purchase")
public class PurchaseController {
    @Autowired
    PurchaseService purchaseService;

    /**
     * 查询所有的供应商
     * @return
     */
    @RequestMapping("/purchase-list")
    @ResponseBody
    public Map<String,Object> selectPurchase(Integer pageNum, Integer pageSize, String search, String type){
        Page<Object> obj = PageHelper.startPage(pageNum,pageSize);
        purchaseService.selectPurchases(search,type);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        return map;
    }

    /**
     * 查询所有的供应商
     * @return
     */
    @RequestMapping("/supplier-list")
    @ResponseBody
    public List<Supplier> selectSupplier(){
        return purchaseService.selectSupplier();
    }

    /**
     * 采购模糊查询供应商
     * @return
     */
    @RequestMapping("/SelectBySupplierName")
    @ResponseBody
    public Map<String,Object> SelectBySupplierName(Integer pageNum, Integer pageSize, String supplierName){
        Page<Object> obj = PageHelper.startPage(pageNum,pageSize);
        purchaseService.SelectBySupplierName(supplierName);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        return map;
    }

    /**
     * 根据供应商名称查询供应商联系人
     * @return
     */
    @RequestMapping("/ContactBySupplierName")
    @ResponseBody
    public List<SupplierContact> ContactBySupplierName(String supplierName){
        return purchaseService.ContactBySupplierName(supplierName);
    }

    /**
     * 根据供应商名称查询供应商联系人
     * @return
     */
    @RequestMapping("/PurchaseOrder")
    @ResponseBody
    public String PurchaseOrder(){
        return purchaseService.PurchaseOrder();
    }


    /**
     * 新增采购单和采购详情
     * @param purchases
     */
    @RequestMapping("/addPurchase")
    @ResponseBody
    public void addPurchase(String purchases){
        JSONObject obj = (JSONObject)JSONObject.parse(purchases);
        List purList = obj.getObject("purList",List.class);
        Purchase purchase = obj.getObject("Purchase",Purchase.class);
        purchaseService.addPurchase(purchase,purList);
    }

    /**
     * 查询需要修改的采购详情
     * @return
     */
    @RequestMapping("/upPurchase")
    @ResponseBody
    public List<PurchaseDetails> selectPurchaseDetails(Integer purchaseId){
        return purchaseService.selectPurchaseDetails(purchaseId);
    }

    /**
     * 查询采购单状态
     * @return
     */
    @RequestMapping("/PurchaseStatus")
    @ResponseBody
    public String PurchaseStatus(Integer purchaseId){
        return purchaseService.PurchaseStatus(purchaseId);
    }

    /**
     * 删除采购单
     * @return
     */
    @RequestMapping("/delPurchase")
    @ResponseBody
    public void delPurchase(Integer purchaseId){
        purchaseService.delPurchase(purchaseId);
    }

}
